//
//  ChargeSerialView.m
//  aiteshangcheng
//
//  Created by caominglei on 2018/4/24.
//  Copyright © 2018年 aite. All rights reserved.
//

#import "ChargeSerialView.h"
#import "UIImage+Category.h"
#import "ChargeStateView.h"
#import "SerialCell.h"
#import "Network.h"
#import "UIView+NoDataShow.h"
@interface ChargeSerialView ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource,SerialCellDelegate>
@property (strong, nonatomic) UILabel *chargetext;
@property (strong, nonatomic) UIButton *searchBtn;
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) UIView *headView;
@property (strong, nonatomic) ChargeStateView *stateView;
@property (nonatomic , strong) UIImageView *noDataIV;
@property (assign, nonatomic) NSInteger index;
@property (strong, nonatomic) NSMutableArray *dataArray;
@end
@implementation ChargeSerialView
static NSString *serialcell = @"serialcell";

- (ChargeStateView *)stateView
{
    if (!_stateView) {
        _stateView = [[ChargeStateView alloc] init];
    }
    return _stateView;
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[SerialCell class] forCellReuseIdentifier:serialcell];
    }
    return _tableView;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.dataArray = [NSMutableArray array];
        [self getSerialList];

        self.backgroundColor = [UIColor whiteColor];
        [self createSubviews];
        [self createSubviewsFrame];
        
    }
    return self;
}

/**
 
 
 key 当前登陆令牌
 id 自增编号（pdr_id）
 

 */
- (void)payOrder
{
//    SerialModel *model = self.dataArray[];
    NSString *url = [NSString stringWithFormat:@"%@%@",REMOTEURL,Recharge_del];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"key"] = DHNetworkManager.shareManager.userAccount.key;
    dic[@"id"] = @"3";

    [Network executeRecharge_delTaskWithUrl:url Dict:dic Success:^(id obj) {
        
    } failed:^(id obj) {
        
    }];
}


- (void)deleteOrder
{
    
}
/*
 key 会员登录key
 当前页curpage 若无此参数默认1；
 pdr_sn 搜索时填写的：充值单号/记录唯一标示
 */

- (void)getSerialList
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"key"] = DHNetworkManager.shareManager.userAccount.key;
    dic[@"curpage"] = @"1";
    dic[@"pdr_sn"] = self.inputfield.text;
    
    NSString *url = [NSString stringWithFormat:@"%@%@",REMOTEURL,Redeposit_list];
    [Network executeGetPredepositTaskWithUrl:url Dict:dic Success:^(id obj) {
        [self.dataArray removeAllObjects];
        [self.noDataIV removeFromSuperview];
        NSArray *dataArray = [NSArray yy_modelArrayWithClass:[SerialModel class] json:obj[@"datas"][@"list"]];
        [self.dataArray addObjectsFromArray:dataArray];
        
        self.amount.text = [NSString stringWithFormat:@"可用金额:%@,冻结金额:%@",obj[@"datas"][@"member_info"][@"available_predeposit"],obj[@"datas"][@"member_info"][@"freeze_predeposit"]];

        if (self.dataArray.count == 0) {
            
            self.noDataIV = [UIView NoDataImg:@"nopicture" withTip:@"您当前还没有相关记录哦~T_T~"];
            self.noDataIV.center = CGPointMake(kScreenWidth * 0.5, 200);
            [self addSubview:self.noDataIV];
            self.tableView.hidden = YES;
        }else{
            self.tableView.hidden = NO;

            [self.tableView reloadData];
        }
    } failed:^(id obj) {
        
    }];
}
- (void)createSubviews
{
    
    UILabel *amount = [YCCommonCtrl commonLableWithFrame:CGRectZero text:@"可用金额:-/--,冻结金额:-/--" color:[UIColor blackColor] font: [UIFont systemFontOfSize:10] textAlignment:NSTextAlignmentLeft];
    [self addSubview:amount];
    amount.backgroundColor = [UIColor orangeColor];
    self.amount = amount;
    
    UIView *headView = [[UIView alloc] init];
    [self addSubview:headView];
    self.headView = headView;
    
    UILabel *chargetext = [YCCommonCtrl commonLableWithFrame:CGRectZero text:@"充值单号" color:[UIColor grayColor] font: [UIFont systemFontOfSize:12] textAlignment:NSTextAlignmentLeft];
    [self.headView addSubview:chargetext];
    chargetext.backgroundColor = [UIColor whiteColor];
    self.chargetext = chargetext;
    
    UIButton *searchBtn = [YCCommonCtrl commonButtonWithFrame:CGRectZero title:@"搜索" color:[UIColor whiteColor] font:[UIFont systemFontOfSize:12] backgroundImage:[UIImage createImageWithColor:[UIColor clearColor]] target:self action:@selector(searchRequest:)];
    searchBtn.backgroundColor = [UIColor groupTableViewBackgroundColor];
    searchBtn.layer.cornerRadius = 4;
    searchBtn.clipsToBounds = YES;
    searchBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [searchBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.headView addSubview:searchBtn];
    self.searchBtn = searchBtn;
    
    UITextField *inputfield = [YCCommonCtrl commonTextFieldWithFrame:CGRectZero placeholder:@"" color:[UIColor blackColor] font:[UIFont systemFontOfSize:12] secureTextEntry:NO delegate:self];
    inputfield.layer.cornerRadius = 4;
    inputfield.clipsToBounds = YES;
    inputfield.layer.borderColor = [UIColor grayColor].CGColor;
    inputfield.layer.borderWidth = 1;
    inputfield.delegate = self;
    inputfield.returnKeyType = UIReturnKeyDone;
    [self.headView addSubview:inputfield];
    self.inputfield = inputfield;
    
    [self addSubview:self.stateView];
    [self addSubview:self.headView];
    [self addSubview:self.tableView];

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self endEditing:YES];
    [self getSerialList];
    return YES;
}
- (void)searchRequest:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(searchSerialWithSearchText:)]) {
        [self.delegate searchSerialWithSearchText:self.inputfield.text];
    }
}
- (void)createSubviewsFrame
{
    [self.amount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(40);
    }];
    
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.amount.mas_bottom).offset(5);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(80);
        make.width.mas_equalTo(kScreenWidth);
    }];
    
    [self.chargetext mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(5);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(75);
    }];
    
    [self.inputfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.chargetext.mas_right).offset(5);
        make.top.mas_equalTo(5);
        make.height.mas_equalTo(30);
        make.right.mas_equalTo(self.searchBtn.mas_left).offset(-5);
    }];
    
    [self.searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-10);
        make.top.mas_equalTo(5);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(50);
    }];
    
    [self.stateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(self.inputfield.mas_bottom).offset(5);
        make.height.mas_equalTo(30);
    }];
    
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.headView.mas_bottom).offset(5);
        make.height.mas_equalTo(400);
    }];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SerialModel *model = self.dataArray[indexPath.row];
    SerialCell *cell = [tableView dequeueReusableCellWithIdentifier:serialcell];
    cell.delegate = self;
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
@end
